import org.joda.time.*
import org.joda.time.format.*


/**
 *
 * this class calls other classes to read the object model from input files
 * and generates the timeline data for:
 *          org-->bpservices-->bps-->sps-->appservices-->appcomponents
 *
 */
class GenOrgBPTimelineData
{
    static def Logger logger = new Logger()
    static def OrgBPReader reader

    static void readBPs(OrgBPReader reader, String inputFilename)
    {
        /////////////////////////////////////////////////////////////////
        // BP Timeline
        logger.info( "Started reading BP Event Pts for:"+ reader.org.name)

        // create the event Pts
        reader.readBPTimeline(new File(inputFilename).getCanonicalPath())

        logger.info( "Finished reading BP Event Pts:"+ reader.org.name)
    }

    static void readSPs(OrgBPReader reader, String inputFilename)
    {
        // SP Timeline
        // now creating the Sub process event Pts
        logger.info( "started reading SP Event Pts")
        reader.readSubProcessTimeline(new File(inputFilename).getCanonicalPath())
        logger.info( "finished reading SP Event Pts")
    }

    static void readAppSvcs(OrgBPReader reader, String inputFilename)
    {
        /////////////////////////////////////////////////////////////////
        // APP Service
        // now reading the App Service event Pts
        logger.info( "reading App Service Events from file")
        reader.readAppServices(new File(inputFilename).getCanonicalPath())
        logger.info( "finished reading App Service Events")
    }

    // JS List Generator
    static void generateJsonForListViews(Organization org)
    {
        // Graph Generator
        /////////////////////////////////////////////////////////////////
        // now creating the JS Lists for Business Processes, sub processes and App Services
        logger.info( "creating BPTimelineList JS List")
        ServicesListCreator listCreator = new ServicesListCreator(org)
        def jsList = listCreator.createBPTimelineList()        
        //logger.info( jsList)
        logger.info( "finished creating BPTimelineList JS List")
        
        logger.info( "starting to write BPTimelineList JS List into file")
        File file = new File(new File("timelinedata/bptimelinelist.js").getCanonicalPath())
        file.write(jsList)
        logger.info( "finished writing BPTimelineList JS List into file")

        logger.info( "creating SPTimelineList JS List")
        jsList = listCreator.createSPTimelineList()        
        //logger.info( jsList)
        logger.info( "finished creating SPTimelineList JS List")

        logger.info( "starting to write SPTimelineList JS List into file")
        file = new File(new File("timelinedata/sptimelinelist.js").getCanonicalPath())
        file.write(jsList)
        logger.info( "finished writing SPTimelineList JS List into file")

        logger.info( "creating AppServicesList JS List")
        jsList = listCreator.createAppServicesList()        
        //logger.info( jsList)
        logger.info( "finished creating AppServicesList JS List")

        logger.info( "starting to write AppServicesList JS List into file")
        file = new File(new File("timelinedata/appserviceslist.js").getCanonicalPath())
        file.write(jsList)
        logger.info( "finished writing AppServicesList JS List into file")
    }
    
    static void generateJsonForTimelineViews(DateTime opDay)
    {
        logger.info( "Operating Date:"+ Utils.getFormattedDateTime(opDay))

        // create the event points for the Business processes
        def bpEventPts = EventPtsUtil.getAllBPEventPtsForOpDate(reader.org, opDay)
        logger.info( "BPEventPts.size:" + bpEventPts.size())

        // now creating the Business process event Pts
        // START JSON
        logger.info( "started creating the JSON BP Event Pts:"+ reader.org.name)
        def bptimelineJson = EventPtsUtil.getEventPtsJson(bpEventPts)
        logger.info( "finished creating the JSON BP Event Pts:"+ reader.org.name)

        // writing to file
        logger.info( "writing BP event Pts to JSON file")
        File file = new File(new File("./timelinedata/bptimeline.js").getCanonicalPath())
        file.write(bptimelineJson.toString())
        logger.info( "finished writing BP eventPts to JSON file")
        // END JSON

        logger.info( "creating SP Event Pts")
        // create the event points for the Business processes
        def spEventPts = EventPtsUtil.getAllSPEventPtsForOpDate(reader.org, opDay)

        // START JSON
        logger.info( "started creating the JSON SP Event Pts:"+ reader.org.name)
        def sptimelineJson = EventPtsUtil.getEventPtsJson(spEventPts)
        //logger.info( "JSON:"+strBuf.toString())
        logger.info( "finished creating the JSON SP Event Pts:"+ reader.org.name)

        logger.info( "writing SP event Pts to JSON file")
        file = new File(new File("./timelinedata/sptimeline.js").getCanonicalPath())
        file.write(sptimelineJson.toString())
        logger.info( "finished writing SP eventPts to JSON file")
        // END JSON

    }

    // Graph Generator
    static void generateGraphs(Organization org)
    {
        // now creating the graphs
        ServicesGraphCreator graphCreator = new ServicesGraphCreator(org)

        graphCreator.createO2BF2BPGraph()
        graphCreator.createBP2SP2ASGraphs()
        graphCreator.createSubProcessGraphs()
        graphCreator.createWholeOrgGraph()
    }

    static void readBPsSPsAppSvcs()
    {
        /////////////////////////////////////////////////////////////////
        readBPs(reader, "./inputdata/orgbptimeline.csv")
        readSPs(reader, "./inputdata/subprocesstimeline.csv")
        readAppSvcs(reader, "./inputdata/appservicestimeline.csv")
    }

    static void main(args)
    {
        // get the Operating Day as today
        //def DateTimeZone timezone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/London"))
        def DateTimeZone timezone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Chicago"))

        // name the organization
        def orgName = "ERCOT"
        reader = new OrgBPReader(orgName)

        // VIP - the logger can either be the servlet-mode logger or from here - 
        //              (defaults to println or other servlet.log(xxx))
        reader.setLogger(logger)

        //def now = new DateTime(timezone)
        // select an Operating Day
        // Operating Day: April 23rd 2009
        def opDay = new DateTime(2009, 4, 23, 12, 0, 0, 0, timezone);
        logger.info( "Generating timeline event data for:"+opDay)

        // read the business process timeline
        GenOrgBPTimelineData.readBPsSPsAppSvcs()

        /////////////////////////////////////////////////////////////////
        // generate timeline views data
        GenOrgBPTimelineData.generateJsonForTimelineViews(opDay)

        /////////////////////////////////////////////////////////////////
        // generate list views data
        GenOrgBPTimelineData.generateJsonForListViews(reader.org)

        /////////////////////////////////////////////////////////////////
        // generate the GRAPHS
        GenTimelineData.generateGraphs(reader.org)
    }
}
